package com.pi.pro.xcmg.am.was.fac.handler;


import com.pi.common.CodesConstants;
import com.pi.common.utils.StringUtils;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.common.vo.act.MoveChangeDto;
import com.pi.pro.xcmg.am.was.edi.wcs.dto.MoveRequestData;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.act.PutawayActionDto;
import com.pi.wms.dto.act.PutawayLineDto;
import com.pi.wms.dto.act.TaskActionDto;
import com.pi.wms.dto.base.LocationLineDto;
import com.pi.wms.entity.*;
import com.pi.wms.service.InventoryLotattrService;
import com.pi.wms.service.LogisticsSeqService;
import com.pi.wms.service.MaterialService;
import com.pi.wms.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 *
 */
@Component
public class BaseDelegateHandler {


    @Autowired
    private MaterialService materialService;

    @Autowired
    private LogisticsSeqService seqService;

    @Autowired
    private InventoryLotattrService lotattrService;

    @Autowired
    private TaskService taskService;


    protected String generate(List<TwaInventoryDetails> storages, LocationLineDto fromLocation, TwmBasLocation paLocation, String batchNumber, MoveChangeDto moveChangeDto,
                              String refLocationCode, String checkBatchNumber, TaskActionDto actionDto){
        String m = "";
        if ((storages==null)||(storages.size()==0)){
            m="inventories is null";
            return m;
        }
        if (paLocation==null){
            m="putaway location is null";
            return m;
        }
        String tasknum=this.seqService.next(WmsConstants.SEQUENCE_NAME_ACT_TASK);
        TwtActTaskList task=null;
        int index=0;
        TwmBasMaterial material=null;
        TwaInventoryLotattr lotno=null;
        try{
            for (TwaInventoryDetails storage:storages){
                if (storage==null){
                    continue;
                }
                index++;
                material=this.materialService.lookup(storage.getMaterialId());
                task=new TwtActTaskList();
                task.setNumber(tasknum);
                task.setNumberSeq(StringUtils.lpad(String.valueOf(index*10),"0",4));
                task.setGlobalClientId(storage.getGlobalClientId());
                task.setDivisionId(storage.getDivisionId());
                if ((fromLocation!=null)&&(fromLocation.getDivisionId()!=null)){
                    task.setDivisionId(fromLocation.getDivisionId());
                }
                task.setCustomerId(storage.getCustomerId());
                task.setMaterialId(storage.getMaterialId());
                task.setDocId(null);
                task.setDocDetailsId(null);
                task.setWaveId(null);
                task.setWaveDetailsId(null);
                task.setType(WmsConstants.TASK_TYPE_PUTAWAY);
                task.setStatus(CodesConstants.COMMON_STATUS_CREATED);
                task.setTaskPrior(WmsConstants.TASK_PRIOR_NORMAL);
                if (!StringUtils.isNullOrBlank(actionDto.getPriority())){
                    task.setTaskPrior(actionDto.getPriority().trim());
                }
                task.setReleaseStatus(CodesConstants.COMMON_RELEASE_STATUS_CREATED);
                if (!StringUtils.isNullOrBlank(actionDto.getReleaseStatus())){
                    task.setReleaseStatus(actionDto.getReleaseStatus().trim());
                }
                task.setDocNumber(null);
                task.setDocLinenumber(null);
                task.setStorageId(storage.getId());
                task.setFromQty(storage.getQty());
                task.setFromQtyEach(task.getFromQty());
                task.setFromPackId(WmsConstants.STANDARD_RULE_PACK_ID);
                if (material!=null){
                    task.setFromPackId(material.getPackId());
                }
                task.setFromUomCode(CodesConstants.UOM_EACH);
                task.setFromPrice(BigDecimal.ZERO);
                task.setFromGrossweight(BigDecimal.ZERO);
                task.setFromNetweight(BigDecimal.ZERO);
                task.setFromCubic(BigDecimal.ZERO);
                task.setFromLotnoId(storage.getLotnoId());
                task.setFromLotnoNumber(storage.getLotnoNumber());
                task.setFromLocId(storage.getLocId()); //
                task.setFromLocSeq(storage.getLocCode());
                if ((fromLocation!=null) && (fromLocation.getLocationId()!=null)){
                    task.setFromLocId(fromLocation.getLocationId()); //
                    task.setFromLocSeq(fromLocation.getLocationCode());
                }
                task.setFromContainNumber(storage.getContainNumber());
                task.setFromTraceNumber(storage.getTraceNumber());
                if (StringUtils.isNullOrBlank(task.getFromTraceNumber())){
                    task.setFromTraceNumber(task.getFromContainNumber());
                }
                task.setToDivisionId(task.getDivisionId());
                task.setToQty(task.getFromQty());
                task.setToQtyEach(task.getToQty());
                task.setToPackId(task.getFromPackId());
                task.setToUomCode(task.getFromUomCode());
                task.setToPrice(task.getFromPrice());
                task.setToGrossweight(task.getFromGrossweight());
                task.setToNetweight(task.getFromNetweight());
                task.setToCubic(task.getFromCubic());
                task.setToLotnoId(task.getFromLotnoId());
                task.setToLotnoNumber(task.getFromLotnoNumber());
                task.setToLocId(paLocation.getId());
                task.setToLocSeq(paLocation.getCode());
                task.setToContainNumber(task.getFromContainNumber());
                task.setToTraceNumber(task.getFromTraceNumber());
                //allocation.setPlantoLocId(allocation.getToLocId());
                task.setPlantoLocId(null);
                task.setPlantoContainNumber(null);
                task.setPlantoTraceNumber(null);
                task.setBatchNumber(batchNumber);
                task.setGroupNumber(checkBatchNumber);
                task.setBulkNumber(checkBatchNumber);
                task.setPrintStatus(CodesConstants.COMMON_STATUS_CREATED);
                task.setRefType(WmsConstants.TRANS_CATEGORY_INVENTORY);
                task.setRefId(null);

                task.setUdf04(null);
                if (!StringUtils.isNullOrBlank(refLocationCode)){
                    task.setUdf04(refLocationCode.trim());
                }
                //
                task.setTaskAction(XcmgAmConstants.TASK_ACTION_STORAGE);
                //
                lotno=this.lotattrService.lookup(storage.getLotnoId());
                this.fill(lotno,task);
                task.setErrorCode(null);
                task.setErrorMessages(null);
                task.setUdf01(null);
                task.setDelFlag(false);
                task.setEditable(WmsConstants.ENABLE_VALUE);
                String worker= SecurityUtils.getLoginUsername();
                if (!StringUtils.isNullOrBlank(actionDto.getWorker())){
                    worker=actionDto.getWorker().trim();
                }
                task.setOpenTime(new Date());
                task.setOpenBy(worker);

                task.setEdiSendStatus(CodesConstants.COMMON_STATUS_CREATED);
                task.setCreatedDate(new Date());
                task.setCreatedBy(worker);
                task.setLastModifiedDate(task.getCreatedDate());
                task.setLastModifiedBy(task.getCreatedBy());
                try{
                    this.taskService.save(task);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }

    protected String generateByPalletize(List<TwtActPalletizeList> palletizes, LocationLineDto fromLocation, TwmBasLocation paLocation, String batchNumber, MoveChangeDto moveChangeDto,
                              String refLocationCode, String checkBatchNumber, TaskActionDto actionDto){
        String m = "";
        if ((palletizes==null)||(palletizes.size()==0)){
            m="inventories is null";
            return m;
        }
        if (paLocation==null){
            m="putaway location is null";
            return m;
        }
        String tasknum=this.seqService.next(WmsConstants.SEQUENCE_NAME_ACT_TASK);
        TwtActTaskList task=null;
        int index=0;
        TwmBasMaterial material=null;
        TwaInventoryLotattr lotno=null;
        try{
            for (TwtActPalletizeList pal:palletizes){
                if (pal==null){
                    continue;
                }
                index++;
                material=this.materialService.lookup(pal.getMaterialId());
                task=new TwtActTaskList();
                task.setNumber(tasknum);
                task.setNumberSeq(StringUtils.lpad(String.valueOf(index*10),"0",4));
                task.setGlobalClientId(pal.getGlobalClientId());
                task.setDivisionId(pal.getDivisionId());
                if ((fromLocation!=null)&&(fromLocation.getDivisionId()!=null)){
                    task.setDivisionId(fromLocation.getDivisionId());
                }
                task.setCustomerId(pal.getCustomerId());
                task.setMaterialId(pal.getMaterialId());
                task.setDocId(pal.getDocumentId());
                task.setDocDetailsId(pal.getDocumentDetailsId());
                task.setWaveId(null);
                task.setWaveDetailsId(null);
                task.setCategory(null);
                task.setType(WmsConstants.TASK_TYPE_PUTAWAY);
                task.setStatus(CodesConstants.COMMON_STATUS_CREATED);
                task.setTaskClass(null);
                task.setTaskPrior(WmsConstants.TASK_PRIOR_NORMAL);
                if (!StringUtils.isNullOrBlank(actionDto.getPriority())){
                    task.setTaskPrior(actionDto.getPriority().trim());
                }
                task.setReleaseStatus(CodesConstants.COMMON_RELEASE_STATUS_CREATED);
                if (!StringUtils.isNullOrBlank(actionDto.getReleaseStatus())){
                    task.setReleaseStatus(actionDto.getReleaseStatus().trim());
                }
                task.setDocNumber(pal.getDocNumber());
                task.setDocLinenumber(pal.getDocLinenumber());
                task.setDocId(pal.getDocumentId());
                task.setDocDetailsId(pal.getDocumentDetailsId());
                task.setStorageId(pal.getId());
                task.setFromQty(pal.getQty());
                task.setFromQtyEach(task.getFromQty());
                task.setFromPackId(WmsConstants.STANDARD_RULE_PACK_ID);
                if (material!=null){
                    task.setFromPackId(material.getPackId());
                }
                task.setFromUomCode(CodesConstants.UOM_EACH);
                task.setFromPrice(BigDecimal.ZERO);
                task.setFromGrossweight(BigDecimal.ZERO);
                task.setFromNetweight(BigDecimal.ZERO);
                task.setFromCubic(BigDecimal.ZERO);
                task.setFromLotnoId(null);
                task.setFromLotnoNumber(null);
                task.setFromLocId(pal.getLocationId()); //
                task.setFromLocSeq(pal.getLocationCode());
                if ((fromLocation!=null) && (fromLocation.getLocationId()!=null)){
                    task.setFromLocId(fromLocation.getLocationId()); //
                    task.setFromLocSeq(fromLocation.getLocationCode());
                }
                task.setFromContainNumber(pal.getContainNumber());
                task.setFromTraceNumber(pal.getTraceNumber());
                if (StringUtils.isNullOrBlank(task.getFromTraceNumber())){
                    task.setFromTraceNumber(task.getFromContainNumber());
                }
                task.setToDivisionId(task.getDivisionId());
                task.setToQty(task.getFromQty());
                task.setToQtyEach(task.getToQty());
                task.setToPackId(task.getFromPackId());
                task.setToUomCode(task.getFromUomCode());
                task.setToPrice(task.getFromPrice());
                task.setToGrossweight(task.getFromGrossweight());
                task.setToNetweight(task.getFromNetweight());
                task.setToCubic(task.getFromCubic());
                task.setToLotnoId(task.getFromLotnoId());
                task.setToLotnoNumber(task.getFromLotnoNumber());
                task.setToLocId(paLocation.getId());
                task.setToLocSeq(paLocation.getCode());
                task.setToContainNumber(task.getFromContainNumber());
                task.setToTraceNumber(task.getFromTraceNumber());
                //allocation.setPlantoLocId(allocation.getToLocId());
                task.setPlantoLocId(null);
                task.setPlantoContainNumber(null);
                task.setPlantoTraceNumber(null);
                task.setBatchNumber(batchNumber);
                task.setGroupNumber(checkBatchNumber);
                task.setBulkNumber(checkBatchNumber);
                task.setPrintStatus(CodesConstants.COMMON_STATUS_CREATED);
                task.setRefType(XcmgAmConstants.TASK_ACTION_PALLETIZE);
                task.setRefId(null);
                //
                this.populate(pal,task);
                task.setErrorCode(null);
                task.setErrorMessages(null);
                task.setDelFlag(false);
                task.setEditable(WmsConstants.ENABLE_VALUE);
                String worker= SecurityUtils.getLoginUsername();
                if (!StringUtils.isNullOrBlank(actionDto.getWorker())){
                    worker=actionDto.getWorker().trim();
                }
                task.setOpenTime(new Date());
                task.setOpenBy(worker);

                task.setEdiSendStatus(CodesConstants.COMMON_STATUS_CREATED);
                task.setCreatedDate(new Date());
                task.setCreatedBy(worker);
                task.setLastModifiedDate(task.getCreatedDate());
                task.setLastModifiedBy(task.getCreatedBy());
                try{
                    this.taskService.save(task);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }

    protected void fill(TwaInventoryLotattr lotno, TwtActTaskList task){
        if ((lotno==null)||(task==null)){
            return;
        }
        task.setLotattr01(lotno.getLotattr01());
        task.setLotattr02(lotno.getLotattr01());
        task.setLotattr03(lotno.getLotattr01());
        task.setLotattr04(lotno.getLotattr01());
        task.setLotattr05(lotno.getLotattr05());
        task.setLotattr06(lotno.getLotattr06());
        task.setLotattr07(lotno.getLotattr07());
        task.setLotattr08(lotno.getLotattr08());
        task.setLotattr09(lotno.getLotattr09());
        task.setLotattr10(lotno.getLotattr10());
        task.setLotattr11(lotno.getLotattr11());
        task.setLotattr12(lotno.getLotattr12());
        task.setLotattr13(lotno.getLotattr13());
        task.setLotattr14(lotno.getLotattr14());
        task.setLotattr15(lotno.getLotattr15());
        task.setLotattr16(lotno.getLotattr16());
    }

    protected void populate(TwtActPalletizeList pal, TwtActTaskList task){
        if ((pal==null)||(task==null)){
            return;
        }
        task.setLotattr01(pal.getLotattr01());
        task.setLotattr02(pal.getLotattr01());
        task.setLotattr03(pal.getLotattr01());
        task.setLotattr04(pal.getLotattr01());
        task.setLotattr05(pal.getLotattr05());
        task.setLotattr06(pal.getLotattr06());
        task.setLotattr07(pal.getLotattr07());
        task.setLotattr08(pal.getLotattr08());
        task.setLotattr09(pal.getLotattr09());
        task.setLotattr10(pal.getLotattr10());
        task.setLotattr11(pal.getLotattr11());
        task.setLotattr12(pal.getLotattr12());
        task.setLotattr13(pal.getLotattr13());
        task.setLotattr14(pal.getLotattr14());
        task.setLotattr15(pal.getLotattr15());
        task.setLotattr16(pal.getLotattr16());

        task.setRefId(pal.getId());
        task.setRefType(XcmgAmConstants.TASK_ACTION_PALLETIZE);
        task.setTaskAction(XcmgAmConstants.TASK_ACTION_PALLETIZE);
        task.setBulkNumber(pal.getRefNumber());

        task.setTaskSchema(pal.getWorker());  //X-WMS
        task.setCategory(pal.getDocType());   //PO

        task.setUdf01(pal.getUdf01());
        task.setUdf02(pal.getUdf02());
        task.setUdf03(pal.getUdf03());
        task.setUdf04(pal.getUdf04());
        task.setUdf05(pal.getUdf05());
        task.setUdf06(pal.getUdf06());
        task.setUdf07(pal.getUdf07());
        task.setUdf08(pal.getUdf08());
        task.setUdf09(pal.getUdf09());
        task.setUdf10(pal.getUdf10());

        task.setCreatedBy(pal.getCreatedBy());
        task.setLastModifiedBy(pal.getLastModifiedBy());
    }

    protected void fill(TwaInventoryDetails storage, PutawayLineDto line, LocationLineDto fromLocation){
        if (storage==null){
            return;
        }
        if (line==null){
            return;
        }
        line.setGlobalClientId(storage.getGlobalClientId());
        line.setDivisionId(storage.getDivisionId());
        line.setContainNumber(storage.getContainNumber());
        line.setRefMaterialId(storage.getMaterialId());
        line.setLotNumber(storage.getLotNumber());

        line.setQuantity(storage.getQty());
        if (fromLocation!=null){
            line.setDivisionId(fromLocation.getDivisionId());
            line.setLocationCode(fromLocation.getLocationCode());
            line.setRefLocationId(fromLocation.getLocationId());
        }
    }

    protected void fill(TwtActPalletizeList palletize, PutawayLineDto line, LocationLineDto fromLocation){
        if (palletize==null){
            return;
        }
        if (line==null){
            return;
        }
        line.setGlobalClientId(palletize.getGlobalClientId());
        line.setDivisionId(palletize.getDivisionId());
        line.setContainNumber(palletize.getContainNumber());
        line.setRefMaterialId(palletize.getMaterialId());
        line.setLotNumber(palletize.getLotNumber());

        line.setQuantity(palletize.getQty());
        if (fromLocation!=null){
            line.setDivisionId(fromLocation.getDivisionId());
            line.setLocationCode(fromLocation.getLocationCode());
            line.setRefLocationId(fromLocation.getLocationId());
        }
    }

    protected void fill(PutawayActionDto paActionDto, MoveRequestData moveChangeDto, TaskActionDto actionDto, TwaInventoryDetails storage, LocationLineDto fromLocation){
        if (paActionDto==null){
            return;
        }
        if (moveChangeDto!=null){
            paActionDto.setCheckHeight(new BigDecimal(moveChangeDto.getHeight()));
            //paActionDto.setCheckLength(moveChangeDto.getLengthValue());
        }
        if (storage!=null){
            paActionDto.setCheckMaterialId(storage.getMaterialId());
            paActionDto.setCheckLotnoId(storage.getLotnoId());
        }
        if (actionDto!=null){
            paActionDto.setWorker(actionDto.getWorker());
        }
        if ((fromLocation!=null) && (fromLocation.getDivisionId()!=null)){
            paActionDto.setCheckDivisionId(fromLocation.getDivisionId());
        }
        paActionDto.setCheckZoneIds(XcmgAmConstants.asrsZoneIds);
    }

}
